package com.lintf.distributed.transaction.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.distributed.transaction.inventory.entity.ProcessedMessage;
import com.lintf.distributed.transaction.inventory.mapper.ProcessedMessageMapper;
import com.lintf.distributed.transaction.inventory.service.ProcessedMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author lintf
 * @description 针对表【t_processed_message】的数据库操作Service实现
 * @createDate 2025-06-13 10:42:35
 */
@Service
public class ProcessedMessageServiceImpl extends ServiceImpl<ProcessedMessageMapper, ProcessedMessage>
        implements ProcessedMessageService {
    @Override
    public boolean isMessageProcessed(String messageId) {
        LambdaQueryWrapper<ProcessedMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessedMessage::getMessageId, messageId);
        return count(queryWrapper) > 0;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public boolean markMessageProcessed(String messageId) {
        ProcessedMessage processedMessage = new ProcessedMessage();
        processedMessage.setMessageId(messageId);
        processedMessage.setProcessedAt(new Date());
        return save(processedMessage);
    }
}




